<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="团队ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="团队名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="regimental">
        <el-input
          v-model="queryParams.regimental"
          placeholder="团长姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="" prop="teamStatus">
        <el-select
          v-model="queryParams.teamStatus"
          placeholder="团队状态"
          clearable
        >
          <el-option :label="'全部'" :value="''"></el-option>
          <el-option :label="'正常'" :value="1"></el-option>
          <el-option :label="'停用'" :value="0"></el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['team:information:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="teamList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="团队ID" align="center" prop="id" />
      <el-table-column label="团队名称" align="center" prop="groupName" />
      <el-table-column label="团长姓名" align="center" prop="regimental" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="累计使用学术币" align="center" prop="currency" />
      <el-table-column label="团队折扣" align="center" prop="discount">
        <template slot-scope="scope">
          {{
            scope.row.discount && scope.row.discount != 100
              ? accMul(scope.row.discount, 0.1) + "折"
              : "无"
          }}
        </template>
      </el-table-column>
      <el-table-column label="团队成员" align="center" prop="teamCount" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="科藤备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleShowTable(scope.row)"
            >查看详情</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handelDiscount(scope.row, scope.$index)"
            >设置折扣</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <zk-dialog
      :visible="zkVisible"
      title="团队折扣"
      onceLabel="团队名称"
      :id="zkItem.id"
      :onceContent="zkItem.groupName"
      :twiceContent="(zkItem.discount && zkItem.discount !==100)? zkItem.discount / 10 : null"
      oncePlaceholder="获取团队名称失败"
      :onceDisabled="true"
      twiceLabel="团队折扣"
      twicePlaceholder="请设置团队折扣"
      twiceType="number"
      @cancel="zkVisible = false"
      @confirm="zkConfirm"
    >
    </zk-dialog>
  </div>
</template>

<script>
import { listTeam } from "@/api/team/information";
import { accMul } from "@/utils/math";
import zkDialog from "@/components/zkDialog";
import {updateTeam} from "@/api/team/information";
import { Message } from "element-ui";
export default {
  name: "Team",
  components: {
    zkDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 团队表格数据
      teamList: [],
      dateRange: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: null,
        regimental: null,
        userId: null,
        email: null,
        phone: null,
        discount: null,
        currency: null,
        status: null,
        remark: null,
      },
      isSearch: false, // 是否是搜索
      // 折扣弹窗
      zkVisible: false,
      //当前修改折扣团队信息
      zkItem: {
        id: null,
        discount: null,
        groupName: null,
        ind:null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    accMul,
    /** 查询团队列表 */
    getList() {
      let params = {};
      if (this.isSearch) {
        params = this.addDateRange(this.queryParams, this.dateRange);
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
        };
      }
      this.loading = true;
      listTeam(params).then((response) => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupName: null,
        regimental: null,
        userId: null,
        email: null,
        phone: null,
        discount: null,
        currency: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.isSearch = false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    handleShowTable(row) {
      console.log(row);
      const tableId = row.id || this.ids[0];
      const groupName = row.groupName || this.groupName[0];
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage(
        "查看[" + groupName + "]团队详情",
        "/team/information/show-detail/index/" + tableId,
        params
      );
    },
    
    //折扣
    handelDiscount(row,ind){
      this.zkItem = Object.assign({}, row);
      this.zkItem.ind = ind;
      if(row.discount === 100){
        this.zkItem.discount = "无";
      }
      this.zkVisible = true;
    },
    zkConfirm(id,discount){
     
      updateTeam({...this.zkItem, discount: discount? discount * 10 : 100}).then(res=>{
        Message.success("修改成功")
        this.teamList[this.zkItem.ind].discount = discount * 10;
        this.zkVisible = false;
      })
    },


    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download(
        "team/information/export",
        {
          ...this.queryParams,
          ids: this.ids.length ? this.ids : undefined,
        },
        `团队信息_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
